home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / interp / lexer.l < prev    next >
Encoding:
Text File  |  1994-06-28  |  4.5 KB  |  202 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: lexer.l,v 1.8 94/06/27 16:32:07 wlott Exp $
  27. *
  28. * This file is the lexer for the debugger.
  29. *
  30. \**********************************************************************/
  31.  
  32.  
  33. %{
  34. #include <stdlib.h>
  35. #include <string.h>
  36.  
  37. #include "mindy.h"
  38. #include "str.h"
  39. #include "sym.h"
  40. #include "num.h"
  41. #include "bool.h"
  42. #include "lexer.h"
  43. #include "parser.h"
  44.  
  45. #undef YY_INPUT
  46. #define YY_INPUT(buf, result, max_size) result = yy_input(buf, max_size)
  47.  
  48. static int yy_input(char *buf, int max_size);
  49.  
  50. static obj_t token_as_address(void);
  51. static obj_t token_as_string(void);
  52. static obj_t token_as_symbol(void);
  53. static obj_t token_as_keyword(void);
  54.  
  55. %}
  56.  
  57. DIGIT        [0-9]
  58. HEXDIGIT    [0-9a-fA-F]
  59. IDENTIFIER    [-!$%&*+/0-9<=>?@A-Z^_a-z~]+
  60.  
  61. %%
  62.  
  63. \(            return tok_LPAREN;
  64. \)            return tok_RPAREN;
  65. "#"[tT]            yylval = obj_True; return tok_LITERAL;
  66. "#"[fF]            yylval = obj_False; return tok_LITERAL;
  67. "$"            yylval = make_fixnum(-1); return tok_DEBUGVAR;
  68. "$$"            yylval = make_fixnum(-2); return tok_DEBUGVAR;
  69. "$"-?{DIGIT}+    yylval = make_fixnum(atoi(yytext+1)); return tok_DEBUGVAR;
  70. "$a"{DIGIT}+    yylval = make_fixnum(atoi(yytext+2)); return tok_ARG;
  71. 0x{HEXDIGIT}+        yylval = token_as_address(); return tok_LITERAL;
  72. -?{DIGIT}+    yylval = make_fixnum(atoi(yytext)); return tok_LITERAL;
  73. \"[^"\\]*(\\.[^"\\])*\"    yylval = token_as_string(); return tok_LITERAL;
  74. {IDENTIFIER}        yylval = token_as_symbol(); return tok_SYMBOL;
  75. {IDENTIFIER}:        yylval = token_as_keyword(); return tok_KEYWORD;
  76. ,            return tok_COMMA;
  77. [ \t\n]+        ;
  78. .            return tok_ERROR;
  79.  
  80. %%
  81.  
  82. static char *readptr;
  83. static int remaining;
  84.  
  85. void lex_init(void)
  86. {
  87.     yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE );
  88. }
  89.  
  90. void lex_setup(char *input, int len)
  91. {
  92.     readptr = input;
  93.     remaining = len;
  94.     yyrestart(NULL);
  95. }
  96.  
  97. static int yy_input(char *buf, int max_size)
  98. {
  99.     if (remaining == 0)
  100.     return YY_NULL;
  101.     if (remaining <= max_size) {
  102.     int count = remaining;
  103.     memcpy(buf, readptr, remaining);
  104.     readptr = NULL;
  105.     remaining = 0;
  106.     return count;
  107.     }
  108.     else {
  109.     memcpy(buf, readptr, max_size);
  110.     readptr += max_size;
  111.     remaining -= max_size;
  112.     return max_size;
  113.     }
  114. }
  115.  
  116. static obj_t token_as_address(void)
  117. {
  118.     unsigned long addr = 0;
  119.     int i;
  120.  
  121.     for (i = 2; i < yyleng; i++) {
  122.     int c = yytext[i];
  123.     if ('0' <= c && c <= '9')
  124.         addr = (addr<<4) + (c-'0');
  125.     else if ('a' <= c && c <= 'f')
  126.         addr = (addr<<4) + 10 + (c-'a');
  127.     else if ('A' <= c && c <= 'F')
  128.         addr = (addr<<4) + 10 + (c-'A');
  129.     else
  130.         abort();
  131.     }
  132.  
  133.     return (obj_t)addr;
  134. }
  135.  
  136. static obj_t token_as_string(void)
  137. {
  138.     int len = yyleng - 2;
  139.     int i;
  140.     char *ptr;
  141.     obj_t str;
  142.  
  143.     for (i = 1; i < yyleng-1; i++)
  144.     if (yytext[i] == '\\') {
  145.         len--;
  146.         i++;
  147.     }
  148.  
  149.     str = alloc_string(len);
  150.     ptr = string_chars(str);
  151.  
  152.     for (i = 1; i < yyleng-1; i++)
  153.     if (yytext[i] == '\\') {
  154.         i++;
  155.         switch (yytext[i]) {
  156.           case 'n':
  157.         *ptr++ = '\n';
  158.         break;
  159.           case '"':
  160.         *ptr++ = '\"';
  161.         break;
  162.           case 't':
  163.         *ptr++ = '\t';
  164.         break;
  165.           case 'b':
  166.         *ptr++ = '\b';
  167.         break;
  168.           case 'r':
  169.         *ptr++ = '\r';
  170.         break;
  171.           default:
  172.         *ptr++ = yytext[i];
  173.         break;
  174.         }
  175.     }
  176.     else
  177.         *ptr++ = yytext[i];
  178.     *ptr = '\0';
  179.  
  180.     return str;
  181. }
  182.  
  183. static obj_t token_as_symbol(void)
  184. {
  185.     char buffer[256];
  186.  
  187.     memcpy(buffer, yytext, yyleng);
  188.     buffer[yyleng] = '\0';
  189.  
  190.     return symbol(buffer);
  191. }
  192.  
  193. static obj_t token_as_keyword(void)
  194. {
  195.     char buffer[256];
  196.  
  197.     memcpy(buffer, yytext, yyleng-1);
  198.     buffer[yyleng-1] = '\0';
  199.  
  200.     return symbol(buffer);
  201. }
  202.